// ?a=1&b=100&name=zhangsan

/**
 * 获取url参数
 * @param {string} name
 * @returns
 */
export function getQuery(name: string) {
	const p = new URLSearchParams(location.search)
	return p.get(name)
}

/**
 * 获取url参数
 * @param {string} name
 * @returns
 */
export function getUrlParam(name: string) {
	const query = location.search.substring(1) // ? 后第一位开始截取
	const paramArr = query.split('&') // ['a=1']
	for (let i = 0; i < paramArr.length; i++) {
		const pair = paramArr[i].split('=') // ['a','1']
		if (pair[0] === name) {
			return pair[1]
		}
	}
	return false
}
/**
 * 对象形式输出
 * @returns
 */
export function queryToObj() {
	const res = {}
	const query = location.search.slice(1) // 去掉 `?`
	query.split('&').forEach((n) => {
		const arr = n.split('=')
		const key = arr[0]
		const val = arr[1]
		res[key] = val
	})
	return res
}

// console.log(getQuery('a'))
// console.log(getUrlParam('b'))
// const { a, b } = queryToObj()
